/*
 * DatabaseServiceImpl.java
 *
 * Created on May 31, 2008, 3:40 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.wirinun.server.database;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.wirinun.client.MainEntryPoint;
import org.wirinun.client.database.DatabaseService;
import org.wirinun.common.Project;

/**
 *
 * @author ETHGGY
 */
public class DatabaseServiceImpl extends RemoteServiceServlet implements
    DatabaseService {

    public Project[]  getProjects() {
        

        HashSet<Project> projectSet = new HashSet<Project>();
        
        try {
            Connection conn = DriverManager.getConnection("jdbc:derby:resource");
            
            Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);
            ResultSet srs = stmt.executeQuery(
                    "SELECT ID, PROJECT FROM PROJECT");
            
            while (srs.next()) {
                String name = srs.getString("PROJECT");
                int index = srs.getInt("ID");
                
                Project p = new Project();
                p.setId(new Long(index));
                p.setName(name);
                
                projectSet.add(p);
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(MainEntryPoint.class.getName()).log(Level.SEVERE, null, ex);
        }
        Project[] projects = null;
        projectSet.toArray(projects);
        
        
        return projects;
        
    }
}
